package com.kk.base.controller.home;

import com.kk.base.entity.home.*;
import com.kk.base.service.home.HomeUserAddressService;
import com.kk.base.service.home.HomeUserIdCardService;
import com.kk.base.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Configuration
@RequestMapping("/home/entryPage")
public class EntryPageController {

    @Autowired
    private HomeUserAddressService homeUserAddressService;
    @Autowired
    private HomeUserIdCardService homeUserIdCardService;

    @RequestMapping("/singletonPage")
    public String singletonPage(Model model) {
        HomeUser homeUser = SessionUtil.getHomeUser();
        HomeUserAddress defaultRecipientsInfo = homeUserAddressService.findByIsDefaultAndHomeUserTypeAndHomeUser_Id(HomeUserAddress.DEFAULT, HomeUserTypeEnum.RECIPIENT,homeUser.getId());
        HomeUserAddress defaultSenderInfo = homeUserAddressService.findByIsDefaultAndHomeUserTypeAndHomeUser_Id(HomeUserAddress.DEFAULT, HomeUserTypeEnum.SENDER,homeUser.getId());
        List<HomeUserAddress> senderInfoList = homeUserAddressService.findByHomeUserTypeAndHomeUser_Id(HomeUserTypeEnum.SENDER, homeUser.getId());
        List<HomeUserAddress> recipientsInfoList = homeUserAddressService.findByHomeUserTypeAndHomeUser_Id(HomeUserTypeEnum.RECIPIENT, homeUser.getId());
        model.addAttribute("senderInfoList",senderInfoList);
        model.addAttribute("recipientsInfoList",recipientsInfoList);
        model.addAttribute("defaultSenderInfo",defaultSenderInfo);
        model.addAttribute("defaultRecipientsInfo",defaultRecipientsInfo);
        model.addAttribute("paymentMethodEnum", PaymentMethodEnum.values());
        model.addAttribute("homeUser",SessionUtil.getHomeUser());
        model.addAttribute("index",2);
        return "home/online_sent_item/singleton_page";
    }

    @RequestMapping("/bulkOrderPage")
    public String bulkOrderPage(Model model) {
        HomeUser homeUser = SessionUtil.getHomeUser();
        HomeUserAddress defaultSenderInfo = homeUserAddressService.findByIsDefaultAndHomeUserTypeAndHomeUser_Id(HomeUserAddress.DEFAULT, HomeUserTypeEnum.SENDER,homeUser.getId());
        List<HomeUserAddress> senderInfoList = homeUserAddressService.findByHomeUserTypeAndHomeUser_Id(HomeUserTypeEnum.SENDER, homeUser.getId());
        model.addAttribute("senderInfoList",senderInfoList);
        model.addAttribute("defaultSenderInfo",defaultSenderInfo);
        model.addAttribute("paymentMethodEnum", PaymentMethodEnum.values());
        model.addAttribute("homeUser",SessionUtil.getHomeUser());
        model.addAttribute("index",2);
        return "home/online_sent_item/bulk_order_page";
    }

    @RequestMapping("/orderManagement")
    public String orderManagement(Model model){
        model.addAttribute("index",3);
        return "home/order_management/page";
    }

    @RequestMapping("/agingQuery")
    public String agingQuery(Model model){
        model.addAttribute("index",10);
        return "home/aging_query/page";
    }



    @RequestMapping("/index")
    public String index(Model model){
        HomeUser homeUser = SessionUtil.getHomeUser();
        HomeUserIdCard byHomeUserId = homeUserIdCardService.findByHomeUser_Id(homeUser.getId());
        String percent = "30%";
        if (homeUser.getName() != null || byHomeUserId != null) {
            percent = "70%";
        }
        if (homeUser.getName() != null && byHomeUserId != null){
            percent = "100%";
        }
        model.addAttribute("percent", percent);
        model.addAttribute("homeUser", homeUser);
        model.addAttribute("byHomeUserId", byHomeUserId);
        model.addAttribute("index",1);
        return "home/index/index";
    }

}
